<template>
  <div class="app-container">
    <el-form :model="queryParams" :inline="true" :rules="rules" ref="queryForm">
      <el-form-item label="标签名">
         <el-input
          v-model="queryParams.tagName"
          placeholder="请输入标签名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品类别">
         <el-select v-model="queryParams.categoryId" filterable clearable placeholder="请选择" @focus="handleCategoryKey">
          <el-option
            v-for="item in categoryList"
            :key="item.value"
            :label="item.categoryKey  + '-' + item.categoryName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tag:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tag:del']"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="list"
      row-key="id"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="60" align="center"></el-table-column>
      <el-table-column prop="tagName" label="标签名称" show-overflow-tooltip/>
      <el-table-column prop="categoryName" label="产品类别" show-overflow-tooltip/>
      <el-table-column prop="remark" label="备注" show-overflow-tooltip/>
      <el-table-column label="创建时间" align="center" prop="createTime" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
                     v-hasPermi="['tag:update']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tag:del']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改标签对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <div style="height: 100px;">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="24">
              <el-form-item label="标签名称" prop="tagName">
                <el-input v-model.trim="form.tagName" placeholder="请输入标签名称"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="产品类别" prop="categoryId">
                <el-select filterable clearable v-model="form.categoryId" style="width:100%" @change="handleChange">
                  <el-option
                    v-for="item in categoryList"
                    :key="item.value"
                    :label="item.categoryName  + '-' + item.categoryKey + '-'+(item.productLine==1?'ZHA':'NEWBEE')"
                    :value="item.categoryId"
                    >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="submitForm2">保存并继续</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
  import {queryTagConfig, saveTagConfig, deleteTagConfig} from '@/api/allData/tagConfig'
  import {queryCategoryList} from '@/api/allData/category'
  import {deepClone} from "@/utils/index";
  
  export default {
    name: 'tag',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非多个禁用
        multiple: true,
        // 数据列表
        list: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 总条数
        total: 0,
        // 查询参数
        queryParams: {
          pageNo: 1,
          pageSize: 10,
          hotelId: ''
        },
        categoryList: [],
        // 表单参数
        form: {
        },
        // 表单校验
        rules: {
          tagName: [
          {required: true, message: "标签名称不能为空", trigger: "blur"}
          ],
          categoryId: [
            {required: true, message: "产品类别不能为空", trigger: "change"}
          ],
          remark: [
            {max: 100, message: '长度不能多于100', trigger: 'blur'}
          ]
        }
      }
    },
    created() {
      this.getList()
      this.handleCategoryKey()
    },
    watch:{
      total(val,oldVal){
        /*  第N页最后一条数据删除后 重新加载上一页 */
        if(this.total>0 && this.total%this.queryParams.pageSize === 0){
          this.queryParams.pageNo = this.queryParams.pageNo - 1
          this.getList(this.queryParams)
        }
      }
    },
    methods: {
      getList() {
        this.loading = true;
        queryTagConfig(this.queryParams).then(response => {
          this.list = response.data;
          this.total = response.page.total;
          this.loading = false;
        });
      },
       handleChange(val){
        var obj = {};
        obj = this.categoryList.find(function(item){
          return item.id === val
        });
        this.form.categoryName = obj.categoryName;
      },
      handleCategoryKey() {
      
        if(this.categoryList.length === 0){
          queryCategoryList({}).then(response => {
            Object.assign(this.categoryList, response.data)
          });
        }
      },
      /* 查询按钮操作 */
      handleQuery() {
        this.getList();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.form.id = '';
        this.open = true;
        this.title = "添加标签";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.form = deepClone(row);
        console.log(this.form);
        this.open = true;
        this.title = "修改标签";
      },
      // 表单重置
      reset() {
        this.form = {
          tagName: '',
          categoryKey: ''
        };
        this.resetForm("form");
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        let dictIds = [];
        if (row.id) {
          dictIds.push(row.id);
        } else {
          dictIds = this.ids
        }
        this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
           return deleteTagConfig({'ids': dictIds});
        }).then(() => {
          
          this.msgSuccess("删除成功");
          this.getList();
        }).catch(function () {
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id !== '') {
              saveTagConfig(this.form).then(response => {
                if (response.code === 0) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.handleQuery();
                }
              });
            } else {
              saveTagConfig(this.form).then(response => {
                debugger
                if (response.code === 0) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.handleQuery();
                }
              })
            }
          }
        });
      },
       /** 提交按钮 */
      submitForm2: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id !== '') {
              saveTagConfig(this.form).then(response => {
                if (response.code === 0) {
                  this.msgSuccess("修改成功");
                  this.form.remark=undefined;
                  this.form.tagName =undefined;
                  this.handleQuery();
                }
              });
            } else {
              saveTagConfig(this.form).then(response => {
                debugger
                if (response.code === 0) {
                  this.msgSuccess("新增成功");
                  this.form.remark=undefined;
                  this.form.tagName =undefined;
                  this.handleQuery();
                }
              })
            }
          }
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.multiple = !selection.length;
      },
    }
  }
</script>
